/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.man.cs.afzal.tcm.terms_sentence_handler;

import ac.man.cs.afzal.tcm.nlp.File_Manager_Impl;
import ac.man.cs.afzal.tcm.nlp.Vector_Builder_Impl;
import ac.man.cs.afzal.tcm.sql_handler.SQL_Handler_Terms_Impl;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hammad
 */
public class Term_to_Occurrence_Map_Unified
{

    private String database_url = "database_url";
    private String username = "username";
    private String password = "password";

    private String MAP_CORPUS = "MAP_CORPUS";
    private String SEED_TERMS_PATH = "SEED_TERMS_PATH";
    private List<String> terms, cs_term_occurences;

    private File_Manager_Impl fileManager = new File_Manager_Impl();
    private Vector_Builder_Impl vectorBuilder = new Vector_Builder_Impl();
    private SQL_Handler_Terms_Impl sqlHandler;

    public Term_to_Occurrence_Map_Unified()
    {
        Properties props = new Properties();
        try
        {
            props.load(new FileInputStream("Term_Classification.properties"));

            database_url = props.getProperty(database_url);
            username = props.getProperty(username);
            password = props.getProperty(password);

            MAP_CORPUS = props.getProperty(MAP_CORPUS);
            SEED_TERMS_PATH = props.getProperty(SEED_TERMS_PATH);
        }
        catch (IOException ex)
        {
            Logger.getLogger(Term_to_Occurrence_Map.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void terms_mapping()
    {
        String[] tables =
        {
            "terms", "term_mapping"
        };
        sqlHandler = new SQL_Handler_Terms_Impl();
        sqlHandler.initialize_parameters(database_url, username, password);
        sqlHandler.connect_db();
        sqlHandler.reset_tables(tables);

        int count1 = 0;
        StringBuffer contents;

        List<String>[] terms_occurences_array;

        File file_dir = new File(MAP_CORPUS);
        String[] list_files = file_dir.list();

        for (String file_name : list_files)
        {
            contents = fileManager.fileReader(MAP_CORPUS + "//" + file_name);
            terms = new ArrayList<String>();
            cs_term_occurences = new ArrayList<String>();

            file_name = file_name.substring(0, file_name.indexOf(".clist"));
            int no_of_terms = count_no_of_terms(contents);

            terms_occurences_array = new List[no_of_terms];
            term_occurrence_array_creator(contents);

            /************************ This loop make entries into Term Mapping ****************************/
            for (int i = 0; i < no_of_terms; i++)
            {
                int term_id;

                terms_occurences_array[i] = new ArrayList<String>();
                terms_occurences_array[i] = vectorBuilder.string_to_vector(cs_term_occurences.get(i), ",");

                for (int k = 0; k < terms_occurences_array[i].size(); k++)
                {
                    String value_occurrence = terms_occurences_array[i].get(k).replaceAll("'", "");
                    List<String> occurrence_ids = sqlHandler.get_occurrence_id_per_occurrence_value(value_occurrence, Integer.toString(0));
                    List<String> doc_ids = sqlHandler.get_doc_ids();

                    for (String occurrence_id : occurrence_ids)
                    {
                        term_id = sqlHandler.get_term_id(terms.get(i), 0);

                        if (term_id == 0)
                        {
                            count1++;
                        }
                        else
                        {
                            String[] doc_table1 =
                            {
                                "Term_ID", "Occurrence_ID"
                            };
                            String[] doc_table_value1 =
                            {
                                Integer.toString(term_id), occurrence_id
                            };
                            sqlHandler.insert_table("Term_Mapping", doc_table1, doc_table_value1);
                        }
                    }
                }
            }
        }
        System.out.println("Unsuccessful Mappings  : " + count1);
    }

    /*
     * This method accepts XML file in Term Mapping format as StringBuffer input, iterate through it 
     * line by line and add terms and comma separated occurrence values in separate lists.
     */
    private void term_occurrence_array_creator(StringBuffer contents)
    {
        int index = 0;
        int i = 0;
        int index1, index2, index3;

        while ((index = contents.indexOf("<term>", index)) != -1)
        {
            i++;
            index = index + 6;
            index1 = contents.indexOf("</term>", index);
            String term = contents.substring(index, index1);
            term = term.replaceAll("'", "");

            String[] doc_table1 =
            {
                "Value", "Doc_ID", "Type"
            };
            String[] doc_table_value1 =
            {
                term, Integer.toString(0), "Corpus"
            };
            sqlHandler.insert_table("Terms", doc_table1, doc_table_value1);

            index = index1 + 7;
            index2 = contents.indexOf("<occurence>", index);
            index3 = contents.indexOf("</occurence>", index2 + 11);
            index = index3 + 17;

            terms.add(term);
            cs_term_occurences.add(contents.substring(index2 + 12, index3 - 1));
        }

    }

    private int count_no_of_terms(StringBuffer contents)
    {
        int index = 0;
        int index1;
        int no_of_terms = 0;

        while ((index = contents.indexOf("<map>", index)) != -1)
        {
            index = index + 5;
            index1 = contents.indexOf("</map>", index);
            index = index1 + 6;
            no_of_terms++;
        }
        return no_of_terms;
    }

    public void loadSeedTerms()
    {
        Vector seed_terms = new Vector();
        Vector occurrence_ids = new Vector();

        StringBuffer contents;

        contents = fileManager.fileReader(SEED_TERMS_PATH);
        seed_terms = vectorBuilder.string_to_vector(contents.toString(), "\r\n");

        for (int i = 0; i < seed_terms.size(); i++)
        {
            String seed_term = seed_terms.elementAt(i).toString();
            occurrence_ids = sqlHandler.get_occurrence_ids_per_occurrence_values(seed_term);

            for (int j = 0; j < occurrence_ids.size(); j++)
            {
                String occurrence_id = occurrence_ids.elementAt(j).toString();
                int term_id = sqlHandler.get_term_id_from_map(occurrence_id);
                sqlHandler.update_term_table(term_id, "task");
            }
        }
        for (int i = 0; i < seed_terms.size(); i++)
        {
            String seed_term = seed_terms.elementAt(i).toString();
            sqlHandler.update_term_table(seed_term, "task");
            seed_term = "'" + seed_term + "'";
            seed_terms.setElementAt(seed_term, i);
        }
        System.out.println(seed_terms.toString());
    }

    public static void main(String[] args)
    {
        Term_to_Occurrence_Map_Unified term_mapping = new Term_to_Occurrence_Map_Unified();
        term_mapping.terms_mapping();
    }
}
